package com.example.demo.Controller;

import com.example.demo.model.Student;
import com.example.demo.model.message;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2022-12-29
 * Time: 15:19
 */

@RestController
public class TestController {
    //@RequestMapping(value = "/hi",method = RequestMethod.POST)
    //@PostMapping("/hi")
    @GetMapping("/hi")
    public String hi(HttpServletRequest request, HttpServletResponse response) {
        return "Hi " + request.getParameter("name") + " ~~~ ";
        //return "hi spring mvc";
    }
    @GetMapping("/demo1")
    public Object demo1(HttpServletRequest request, HttpServletResponse response) {
        return "Hi " + request.getParameter("name") + " ~~~ ";
        //return "hi spring mvc";
    }

    //传1个参数
    @GetMapping("/test999")
    public int test1(Integer val) {
        return val;
    }

    //参数传递对大小写敏感-->通过对象传参
    @GetMapping("/test888")
    public String test2(Student student) {
        return student.toString();
    }

    //获取到前端传递来的参数
    @RequestMapping("/login")
    public String login(String name, String password) {
        return "<h1>" + "登陆账号 : " + name + "登录密码 : " + password + "</h1>";
    }

    //获取到前端传递来的参数
    @RequestMapping("/index")
    public String login(message ms) {
        return "<h1>" + ms + "</h1>";
    }

    //获取ajax请求
    @RequestMapping("/login2")
    public HashMap<Object, Object> login2(@RequestBody String name, String password) {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("name", name);
        map.put("password", password);
        return map;
    }

    //获取Json对象
    @RequestMapping("/login3")
    public HashMap<Object, Object> login3(Student student) {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("name", student.getName());
        map.put("password", student.getPassword());
        return map;
    }

    //拿上传的文件
    @RequestMapping("/reg1")
    public String reg1(String name, @RequestPart("myfile")MultipartFile file) throws IOException {
        file.transferTo(new File("./demo.png"));
        return "ok";
    }

//    //获取cookie值
//    @RequestMapping("/getCookie")
//    public String getCookie(@CookieValue("sfx")String sfx){
//        return "sfx : " + sfx;
//    }
}